﻿using SCG.General;
using SCG.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine;

namespace TorchesTombs
{
    public class FireSprite : MapObjectSprite
    {
        //constructor
        public FireSprite(MapObject mo, int particles) 
            : base(mo)
        {
            Shape = new RectangleF(0, 0, 64, 64);
            ClickShape = Shape;

            if (_colorizedFire.Count == 0)
            {
                Bitmap baseFire = Properties.Resources.fire;

                //create colorized copies
                for (int g = 127; g < 256; g += 32)
                {
                    Bitmap colorized = ImageUtils.ColorizeBitmap(baseFire, Color.FromArgb(255, g, 0), Color.Fuchsia);
                    _colorizedFire.Add(colorized);
                }
            }
            while (particles > 0)
            {
                particles--;
                FireParticle p = new FireParticle();
                p.Initialize();
                p.Image = _colorizedFire.RandomValue;
                _particles.Add(p);
            }
        }

        //render
        public override void Render(Graphics g)
        {
            if (Surface == null)
                return;
            foreach (FireParticle p in _particles)
            {
                g.DrawImage(p.Image, (int)(X + p.X - Surface.OffsetX - 16), (int)(Y + p.Y - Surface.OffsetY - 8));
                p.Y -= Globals.Random.NextDouble() * 1.5;
                p.X += Globals.Random.NextDouble() * 3.0 - 1.5;
                p.Lifetime--;
                if (p.Lifetime <= 0)
                {
                    p.Initialize();
                    p.Image = _colorizedFire.RandomValue;
                }
            }
        }

        //private members
        private List<FireParticle> _particles = new List<FireParticle>();
        private static RandomList<Bitmap> _colorizedFire = new RandomList<Bitmap>();
    }
}
